package ma.co.ba.BusinessApplications.presentation.controllers.activites;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ma.co.ba.BusinessApplications.business.activites.ActiviteBusiness;
import ma.co.ba.BusinessApplications.business.commons.CommonBusiness;
import ma.co.ba.BusinessApplications.business.prestations.PrestationBusiness;
import ma.co.ba.BusinessApplications.business.projets.ProjetBusiness;
import ma.co.ba.BusinessApplications.business.utilisateurs.UtilisateurBusiness;
import ma.co.ba.BusinessApplications.model.Activite;
import ma.co.ba.BusinessApplications.model.Prestation;
import ma.co.ba.BusinessApplications.model.Projet;
import ma.co.ba.BusinessApplications.model.Utilisateur;
import ma.co.ba.BusinessApplications.presentation.components.PrestationEditor;
import ma.co.ba.BusinessApplications.presentation.components.ProjetEditor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/activites")
public class ActiviteController {

	@Autowired
	CommonBusiness commonBusiness;

	@Autowired
	ActiviteBusiness activiteBusiness;

	@Autowired
	UtilisateurBusiness utilisateurBusiness;

	@Autowired
	ProjetBusiness projetBusiness;

	@Autowired
	PrestationBusiness prestationBusiness;

	@Autowired
	PrestationEditor prestationEditor;

	@Autowired
	ProjetEditor projetEditor;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
		binder.registerCustomEditor(Prestation.class, this.prestationEditor);
		binder.registerCustomEditor(Projet.class, this.projetEditor);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String activitesPage(ModelMap model) {

		return "activites";
	}
	
	@RequestMapping(value="/all", method = RequestMethod.GET)
	public String allactivitesPage(ModelMap model) {
		return "allActivites";
	}
	
//	@RequestMapping(value = "/test", method = RequestMethod.GET)
//	public @ResponseBody
//	void test(ModelMap model, Authentication a) {
//		String auths = a.getAuthorities().toString();
//		System.out.println(auths.contains("Directeur"));
//	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(BindingResult result, SessionStatus status) {

		return "redirect:/activites";
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String newActiviteModal(ModelMap model) {

		return "newActivite";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public String newActiviteSubmit(
			@ModelAttribute("newActivite") Activite newActivite,
			ModelMap model, Principal principal) {

		Utilisateur user = (Utilisateur) utilisateurBusiness
				.getUserByFullName(principal.getName());
		newActivite.setConsultant(user);
		commonBusiness.saveEntity(newActivite);
		return "redirect:/activites";
	}

	@ModelAttribute("projetsList")
	public Map<String, String> getProjetsList() {
		List<Projet> activeProjets = null;
		activeProjets = projetBusiness.getAllProjets();

		Map<String, String> projetsList = new HashMap<String, String>();
		if (activeProjets != null) {
			for (Projet projet : activeProjets) {
				projetsList.put(projet.getId() + "", projet.getNomProjet());
			}
		}
		return projetsList;
	}

	@ModelAttribute("prestationsList")
	public Map<String, String> getPrestationsList() {
		List<Prestation> prestations = null;
		prestations = prestationBusiness.getAllPrestations();

		Map<String, String> prestationsList = new HashMap<String, String>();
		if (prestations != null) {
			for (Prestation prestation : prestations) {
				prestationsList.put(prestation.getId() + "",
						prestation.getPrestationName());
			}
		}
		return prestationsList;
	}

	@ModelAttribute("newActivite")
	public Activite getNewActivite() {
		return new Activite();
	}

	@ModelAttribute("mesActivites")
	public List<Activite> getMyActivities(Principal principal, Authentication a) {
		String name = principal.getName();
		if ( a.getAuthorities().toString().contains("Directeur")||"admin".equals(name))
		{
		return activiteBusiness.getAllActivites();	
		}
		else
		{
		return activiteBusiness.getAllActivitesByUser(name);
		}
	}

	
	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
	
}
